package com.chaihu.bean.entity.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.UUID;

@Slf4j
@Getter
@Setter
@NoArgsConstructor
public class TokenInfoDto implements Serializable {
    private String userId;
    private String userName;
    private String channelNo;
    private String mobile;
    private String createTime;

    public TokenInfoDto(String userId, String channelNo, String userName, String mobile) {
        this.userId = userId;
        this.channelNo = channelNo;
        this.userName = userName;
        this.mobile = mobile;
    }

    public String toPlainString() {
        return JSON.toJSONString(this);
    }


    public static TokenInfoDto convertToBean(String value) {
        TokenInfoDto tokenInfoDto = new TokenInfoDto();
        if (StringUtils.isEmpty(value)) {
            return tokenInfoDto;
        }

        try {
            tokenInfoDto = JSON.parseObject(value, TokenInfoDto.class);
        } catch (JSONException e) {
            log.error("json解析报错：{}", value, e);
            return tokenInfoDto;
        }
        return tokenInfoDto;
    }

    public String generateJdi() {
        String jdi = "jdi-" + UUID.randomUUID().toString().replace("-", "");
        if (!StringUtils.isEmpty(this.getUserId())) {
            jdi += "-" + this.getUserId();
        }
        if (!StringUtils.isEmpty(this.getChannelNo())) {
            jdi += "-" + this.getChannelNo();
        }
        log.info("token jdi : {}, {}", jdi, JSON.toJSONString(this));
        return jdi;
    }

}
